#ifndef JMUTEX_H
#define JMUTEX_H

#include <JXQCore/JXQGlobal>
#include <mutex>

namespace JXQ
{

class JMultiMutexPirvate;

class JXQLIB_EXPORT JMultiMutex
{
public:
	explicit JMultiMutex(bool autoUnlock = false);
	~JMultiMutex();

public:
	void lock(bool block = true);
	bool tryLock();
	void unlock();

public:
	void setAutoUnlock(bool autoUnlock = true);
	bool autoUnlock() const;

private:
	J_DISABLE_COPY(JMultiMutex)
	JMultiMutexPirvate *d_ptr;
};

// dev : read write lock

#define J_MUTEX_LOCKER(std_mutex, ...) \
	std::unique_lock<std::mutex> __mutex_locker__##__VA_ARGS__ \
	(J_CCT(std::mutex&, std_mutex)); \
	J_UNUSED(__mutex_locker__##__VA_ARGS__)

#define J_MUTEX_LOCKER_TYPE(mutexType, _mutex, ...) \
	std::unique_lock<mutexType> __mutex_locker__##__VA_ARGS__ \
	(J_CCT(std::mutex&, _mutex)); \
	J_UNUSED(__mutex_locker__##__VA_ARGS__)

#define J_MUTEX_LOCKER_RELOCK(...) \
	__mutex_locker__##__VA_ARGS__.lock()

#define J_MUTEX_LOCKER_UNLOCK(...) \
	__mutex_locker__##__VA_ARGS__.unlock()

} //namespace JXQ


#endif //JMUTEX_H
